package com.ebt.m.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by andy.wang on 2016/8/10.
 */
public class AgeSeekBar extends View {
    private Paint rectPaint;
    private Paint backgroundPaint;
    private Paint agePaint;
    private Paint progressPaint;
    private int rectColor;
    private Drawable mThumb;
    private Rect backgroundRect;
    private Rect progressRect;
    private int maxProgress = 100;
    private int mWidth;
    private int backgroundPadding;
    private Rect currentRect;
    private GestureDetector gestureDetector;
    private int mProgress;
    private int thumbOffset = 10;
    private int paddingTop = 45;
    private OnProgressChangedListner onProgressChangedListner;
    private int seekBarMinAge;
    private int textMaxWidth;
    private int deltaTextWidth;
    private int ageWithThumbWidth;


    public AgeSeekBar(Context context) {
        this(context, null);
    }

    public AgeSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AgeSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
        gestureDetector = new GestureDetector(context, new GestureListner());
    }

    private class GestureListner implements GestureDetector.OnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            float rawX = e.getX();
            int left = caculatorBound((int) (rawX - backgroundPadding));
            currentRect.left = left;
            currentRect.right = left + ageWithThumbWidth;
            invalidate();
            changeProgress();
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            int left = caculatorBound((int) (currentRect.left - distanceX));
            currentRect.left = left;
            currentRect.right = left + ageWithThumbWidth;
            invalidate();
            changeProgress();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    }

    private int caculatorBound(int i) {
        if (i < 0) return 0;
        if (i > mWidth - 2 * backgroundPadding) return mWidth - 2 * backgroundPadding;
        return i;
    }

    private void initPaint() {
        rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rectPaint.setColor(rectColor);
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(Color.parseColor("#dddddd"));
        agePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        agePaint.setColor(Color.parseColor("#555555"));
        agePaint.setTextSize(36);
        agePaint.setTextAlign(Paint.Align.CENTER);
        textMaxWidth = (int) agePaint.measureText("999");
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setColor(Color.parseColor("#45bf95"));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        backgroundRect = new Rect(backgroundPadding, currentRect.bottom + thumbOffset, mWidth - backgroundPadding, currentRect.bottom + thumbOffset + 18);
        progressRect = new Rect(backgroundPadding, currentRect.bottom + thumbOffset, backgroundPadding, currentRect.bottom + thumbOffset + 18);
    }

    public void setRectColor(int color) {
        this.rectColor = color;
    }

    public void setThumbDrawable(@DrawableRes int drawable) {
        mThumb = getResources().getDrawable(drawable);
        deltaTextWidth = mThumb.getIntrinsicWidth() > textMaxWidth ? 0 : textMaxWidth - mThumb.getIntrinsicWidth();
        ageWithThumbWidth = Math.max(mThumb.getIntrinsicWidth(), textMaxWidth);
        currentRect = new Rect(0, paddingTop, Math.max(mThumb.getIntrinsicWidth(), textMaxWidth), mThumb.getIntrinsicHeight() + paddingTop);
        mThumb.setBounds(currentRect);
        backgroundPadding = currentRect.width() / 2;
    }

    public void setMaxProgress(int maxProgress, int seekBarMinAge) {
        this.maxProgress = maxProgress;
        this.seekBarMinAge = seekBarMinAge;
    }

    public int getSelectedAge() {
        return seekBarMinAge + mProgress;
    }

    public int getProgress() {
        return mProgress;
    }

    public void setProgress(int progress) {
        if (mProgress == progress || progress > maxProgress) return;
        mProgress = progress;
        int left = progress * backgroundRect.width() / maxProgress;
        currentRect.left = left;
        currentRect.right = left + ageWithThumbWidth;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawProgress(canvas);
        drawThumb(canvas);
    }

    private void drawThumb(Canvas canvas) {
        canvas.save();
        mThumb.setBounds(currentRect.left + deltaTextWidth / 2, currentRect.top, currentRect.right - deltaTextWidth / 2, currentRect.bottom);
        mThumb.draw(canvas);
        canvas.drawText(getSelectedAge() + "", currentRect.centerX() - 5, paddingTop - 9, agePaint);
        canvas.restore();
    }

    private void drawProgress(Canvas canvas) {
        progressRect.right = currentRect.centerX();
        canvas.drawRect(progressRect, progressPaint);
    }

    private void drawBackground(Canvas canvas) {
        canvas.drawRect(backgroundRect, backgroundPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return gestureDetector.onTouchEvent(event);
    }

    public void setOnProgressChangedLisnter(OnProgressChangedListner lisnter) {
        this.onProgressChangedListner = lisnter;
    }

    public interface OnProgressChangedListner {
        void onProgressChanged(int progress);
    }

    private void changeProgress() {
        int progress = (currentRect.centerX() - backgroundPadding) * maxProgress / backgroundRect.width();
        mProgress = progress;
        if (onProgressChangedListner != null) {
            onProgressChangedListner.onProgressChanged(progress);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, paddingTop + mThumb.getIntrinsicHeight() + thumbOffset + 18);
    }
}
